package com.injurydetection.www.view;


import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.injurydetection.www.MyApplication;
import com.injurydetection.www.R;
import com.injurydetection.www.adapter.JianceMainAdapter;
import com.injurydetection.www.bean.DataBean;
import com.injurydetection.www.bean.JianshangMain;
import com.injurydetection.www.bean.MessEvent;
import com.injurydetection.www.control.ExamineListControl;
import com.injurydetection.www.greendao.DataBeanDao;
import com.injurydetection.www.widget.DetailPopu;
import com.injurydetection.www.widget.MyPopu;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.greenrobot.greendao.query.LazyList;
import org.greenrobot.greendao.query.WhereCondition;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 * 检伤记录
 */
public class ExamineListFragment extends Fragment implements View.OnClickListener {
    private int x = R.layout.fragment_examine_list;
    @BindView(R.id.cancel_tv)
    TextView cancelTv;
    @BindView(R.id.changan_rl)
    RelativeLayout changanRl;
    @BindView(R.id.select_rl)
    RelativeLayout selectRl;
    @BindView(R.id.selectall_tv)
    TextView selectallTv;
    //    @BindView(R.id.daochu_tv)
//    TextView daochuTv;
    @BindView(R.id.search_img)
    ImageView searchImg;
    @BindView(R.id.cancel_search_tv)
    TextView cancelSearchTv;
    @BindView(R.id.search_et)
    EditText searchEt;
    @BindView(R.id.search_rl)
    RelativeLayout searchRl;
    @BindView(R.id.date)
    TextView date;
    @BindView(R.id.empty_ll)
    LinearLayout emptyLl;
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.refresh)
    SmartRefreshLayout refresh;
    @BindView(R.id.shangqing)
    TextView shangqing;
    @BindView(R.id.shanchu_tv)
    TextView shanchuTv;
    @BindView(R.id.dongzuo)
    TextView dongzuo;
    @BindView(R.id.yuyan)
    TextView yuyan;
    @BindView(R.id.more_img)
    ImageView moreImg;
    @BindView(R.id.xuexing)
    TextView xuexing;
    @BindView(R.id.eye)
    TextView eye;
    private LinearLayoutManager mLinearLayoutManager;
    private Unbinder mUnbinder;
    private List<JianshangMain> mJianshangMainList;
    private boolean isShowCheckBox = true;
    private JianceMainAdapter adapter;
    private static boolean isChoose;
    private List<DataBean> list;
    private String bloodType = "999";
    private int eyeType = 999, sayType = 999, sportType = 999, scoreType = 999;
    //定义三种时间的选择   1.全部   2.今天   3.自选
    private long startTime = 0L, endTime = new Date().getTime();

    public static boolean isIsChoose() {
        return isChoose;
    }

    public static void setIsChoose(boolean isChoose) {
        ExamineListFragment.isChoose = isChoose;
    }

    public ExamineListFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    private static final String TAG = "ExamineListFragment";

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void test(MessEvent messEvent) {
        Log.e(TAG, "test: " + messEvent.getStartTime());
        switch (messEvent.getType()) {
            case 0://全部
            {
                startTime = 0L;
                endTime = new Date().getTime();
                WhereCondition conditionScoreLevel;
                if (scoreType == 999) {
                    conditionScoreLevel = DataBeanDao.Properties.ScoreLevel.in(1, 2, 3, 4);
                } else {
                    conditionScoreLevel = DataBeanDao.Properties.ScoreLevel.eq(scoreType);
                }

                WhereCondition conditionSport;
                if (sportType == 999) {
                    conditionSport = DataBeanDao.Properties.Sport.in(1, 2, 3, 4, 5, 6);
                } else {
                    conditionSport = DataBeanDao.Properties.Sport.eq(sportType);
                }

                WhereCondition conditionSay;
                if (sayType == 999) {
                    conditionSay = DataBeanDao.Properties.Say.in(1, 2, 3, 4, 5);
                } else {
                    conditionSay = DataBeanDao.Properties.Say.eq(sayType);
                }

                WhereCondition conditionEye;
                if (eyeType == 999) {
                    conditionEye = DataBeanDao.Properties.Eye.in(1, 2, 3, 4);
                } else {
                    conditionEye = DataBeanDao.Properties.Eye.eq(eyeType);
                }

                WhereCondition conditionXuexing;
                if (bloodType.equals("999")) {
                    conditionXuexing = DataBeanDao.Properties.BloodType.in("A型", "B型", "O型", "AB型");
                } else {
                    conditionXuexing = DataBeanDao.Properties.BloodType.eq(bloodType);
                }

                WhereCondition conditionTime = DataBeanDao.Properties.CreateTime.le(endTime);//<=
                WhereCondition conditionTime2 = DataBeanDao.Properties.CreateTime.ge(startTime);//>=

                list = MyApplication.getDaoSession().getDataBeanDao().queryBuilder()
                        .where(conditionScoreLevel, conditionSport, conditionSay, conditionEye, conditionXuexing, conditionTime, conditionTime2)
                        .orderDesc(DataBeanDao.Properties.CreateTime)
                        .build()
                        .list();

                adapter.setDataBeans(list);
                adapter.notifyDataSetChanged();
                break;
            }
            case 1://今天
            {
                startTime = messEvent.getStartTime();
                endTime = messEvent.getEndTime();
                WhereCondition conditionScoreLevel;
                if (scoreType == 999) {
                    conditionScoreLevel = DataBeanDao.Properties.ScoreLevel.in(1, 2, 3, 4);
                } else {
                    conditionScoreLevel = DataBeanDao.Properties.ScoreLevel.eq(scoreType);
                }

                WhereCondition conditionSport;
                if (sportType == 999) {
                    conditionSport = DataBeanDao.Properties.Sport.in(1, 2, 3, 4, 5, 6);
                } else {
                    conditionSport = DataBeanDao.Properties.Sport.eq(sportType);
                }

                WhereCondition conditionSay;
                if (sayType == 999) {
                    conditionSay = DataBeanDao.Properties.Say.in(1, 2, 3, 4, 5);
                } else {
                    conditionSay = DataBeanDao.Properties.Say.eq(sayType);
                }

                WhereCondition conditionEye;
                if (eyeType == 999) {
                    conditionEye = DataBeanDao.Properties.Eye.in(1, 2, 3, 4);
                } else {
                    conditionEye = DataBeanDao.Properties.Eye.eq(eyeType);
                }

                WhereCondition conditionXuexing;
                if (bloodType.equals("999")) {
                    conditionXuexing = DataBeanDao.Properties.BloodType.in("A型", "B型", "O型", "AB型");
                } else {
                    conditionXuexing = DataBeanDao.Properties.BloodType.eq(bloodType);
                }

                WhereCondition conditionTime = DataBeanDao.Properties.CreateTime.le(endTime);//<=
                WhereCondition conditionTime2 = DataBeanDao.Properties.CreateTime.ge(startTime);//>=

                list = MyApplication.getDaoSession().getDataBeanDao().queryBuilder()
                        .where(conditionScoreLevel, conditionSport, conditionSay, conditionEye, conditionXuexing, conditionTime, conditionTime2)
                        .orderDesc(DataBeanDao.Properties.CreateTime)
                        .build()
                        .list();
                Log.e("onItemSelect: ", list.size() + "");

                adapter.setDataBeans(list);
                adapter.notifyDataSetChanged();
                break;
            }
            case 2://选择的日期
            {
                startTime = messEvent.getStartTime();
                endTime = messEvent.getEndTime();
                WhereCondition conditionScoreLevel;
                if (scoreType == 999) {
                    conditionScoreLevel = DataBeanDao.Properties.ScoreLevel.in(1, 2, 3, 4);
                } else {
                    conditionScoreLevel = DataBeanDao.Properties.ScoreLevel.eq(scoreType);
                }

                WhereCondition conditionSport;
                if (sportType == 999) {
                    conditionSport = DataBeanDao.Properties.Sport.in(1, 2, 3, 4, 5, 6);
                } else {
                    conditionSport = DataBeanDao.Properties.Sport.eq(sportType);
                }

                WhereCondition conditionSay;
                if (sayType == 999) {
                    conditionSay = DataBeanDao.Properties.Say.in(1, 2, 3, 4, 5);
                } else {
                    conditionSay = DataBeanDao.Properties.Say.eq(sayType);
                }

                WhereCondition conditionEye;
                if (eyeType == 999) {
                    conditionEye = DataBeanDao.Properties.Eye.in(1, 2, 3, 4);
                } else {
                    conditionEye = DataBeanDao.Properties.Eye.eq(eyeType);
                }

                WhereCondition conditionXuexing;
                if (bloodType.equals("999")) {
                    conditionXuexing = DataBeanDao.Properties.BloodType.in("A型", "B型", "O型", "AB型");
                } else {
                    conditionXuexing = DataBeanDao.Properties.BloodType.eq(bloodType);
                }

                WhereCondition conditionTime = DataBeanDao.Properties.CreateTime.le(endTime);//<=
                WhereCondition conditionTime2 = DataBeanDao.Properties.CreateTime.ge(startTime);//>=

                list = MyApplication.getDaoSession().getDataBeanDao().queryBuilder()
                        .where(conditionScoreLevel, conditionSport, conditionSay, conditionEye, conditionXuexing, conditionTime, conditionTime2)
                        .orderDesc(DataBeanDao.Properties.CreateTime)
                        .build()
                        .list();

                adapter.setDataBeans(list);
                adapter.notifyDataSetChanged();
                break;
            }
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_examine_list, container, false);
        mUnbinder = ButterKnife.bind(this, view);
        initListener();
        initData();

        ExamineListControl.change(getContext(), shangqing, false);
        ExamineListControl.change(getContext(), dongzuo, false);
        ExamineListControl.change(getContext(), yuyan, false);
        ExamineListControl.change(getContext(), eye, false);
        ExamineListControl.change(getContext(), xuexing, false);
        Drawable drawable = ContextCompat.getDrawable(getContext(), R.drawable.icon_date);
        drawable.setBounds(0, 0, 45, 45);
        date.setCompoundDrawables(null, null, drawable, null);

        // Inflate the layout for this fragment
        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mUnbinder.unbind();
        EventBus.getDefault().unregister(this);
    }

    private void initData() {
        mJianshangMainList = new ArrayList<>();
    }

    private void initRvAdapter() {
        mLinearLayoutManager = new LinearLayoutManager(getContext());
        rv.setLayoutManager(new GridLayoutManager(getContext(), 4));
        adapter = new JianceMainAdapter(list, getContext());
        adapter.setOnLongClickCallBack(new JianceMainAdapter.OnLongClickCallBack() {
            @Override
            public void onLongClick() {
                changanRl.setVisibility(View.VISIBLE);
                selectRl.setVisibility(View.GONE);
                if (isShowCheckBox) {
                    adapter.setShowCheckBox(true);
                    refreshUI();
                } else {
                    adapter.setShowCheckBox(false);
                    refreshUI();
                }
                isShowCheckBox = !isShowCheckBox;
            }
        });

        adapter.setOnItemClick(new JianceMainAdapter.OnItemClick() {
            @Override
            public void onItemClick(int position) {
                list = MyApplication.getDaoSession().getDataBeanDao().queryBuilder().orderDesc(DataBeanDao.Properties.CreateTime).list();
                DetailPopu detailPopu = new DetailPopu(getContext(), position, list);
                detailPopu.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
                detailPopu.showAtLocation(selectRl, Gravity.BOTTOM, 80, 0);

//                CENTER_HORIZONTAL
            }
        });
        rv.setAdapter(adapter);
    }

    private void initListener() {
        shangqing.setOnClickListener(this);
        yuyan.setOnClickListener(this);
        dongzuo.setOnClickListener(this);
        eye.setOnClickListener(this);
        xuexing.setOnClickListener(this);
        cancelTv.setOnClickListener(this);
        selectallTv.setOnClickListener(this);
//        daochuTv.setOnClickListener(this);
        searchImg.setOnClickListener(this);
        cancelSearchTv.setOnClickListener(this);
        date.setOnClickListener(this);
        shanchuTv.setOnClickListener(this);
        searchEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                list = MyApplication.getDaoSession().getDataBeanDao().queryBuilder()
                        .whereOr(DataBeanDao.Properties.Number.like("%" + s.toString() + "%"), DataBeanDao.Properties.Name.like("%" + s.toString() + "%"))
                        .orderDesc(DataBeanDao.Properties.CreateTime)
                        .build().list();

                adapter.setDataBeans(list);

                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.shangqing:
                ExamineListControl.showPopu(getContext(), shangqing, R.menu.item_pingfen, new ExamineListControl.ItemSelect() {
                    @Override
                    public void onItemSelect(MenuItem item) {
                        switch (item.getTitle().toString()) {
                            case "轻伤":
                                scoreType = 1;
                                break;
                            case "中度伤":
                                scoreType = 2;
                                break;
                            case "重症伤":
                                scoreType = 3;
                                break;
                            case "危重症伤":
                                scoreType = 4;
                                break;
                            default:
                                scoreType = 999;
                                break;
                        }
                        Log.e(TAG, "scoreType: " + scoreType);

                        WhereCondition conditionScoreLevel;
                        if (scoreType == 999) {
                            conditionScoreLevel = DataBeanDao.Properties.ScoreLevel.in(1, 2, 3, 4);
                        } else {
                            conditionScoreLevel = DataBeanDao.Properties.ScoreLevel.eq(scoreType);
                        }

                        WhereCondition conditionSport;
                        if (sportType == 999) {
                            conditionSport = DataBeanDao.Properties.Sport.in(1, 2, 3, 4, 5, 6);
                        } else {
                            conditionSport = DataBeanDao.Properties.Sport.eq(sportType);
                        }

                        WhereCondition conditionSay;
                        if (sayType == 999) {
                            conditionSay = DataBeanDao.Properties.Say.in(1, 2, 3, 4, 5);
                        } else {
                            conditionSay = DataBeanDao.Properties.Say.eq(sayType);
                        }

                        WhereCondition conditionEye;
                        if (eyeType == 999) {
                            conditionEye = DataBeanDao.Properties.Eye.in(1, 2, 3, 4);
                        } else {
                            conditionEye = DataBeanDao.Properties.Eye.eq(eyeType);
                        }

                        WhereCondition conditionXuexing;
                        if (bloodType.equals("999")) {
                            conditionXuexing = DataBeanDao.Properties.BloodType.in("A型", "B型", "O型", "AB型");
                        } else {
                            conditionXuexing = DataBeanDao.Properties.BloodType.eq(bloodType);
                        }

                        WhereCondition conditionTime = DataBeanDao.Properties.CreateTime.le(endTime);//<=
                        WhereCondition conditionTime2 = DataBeanDao.Properties.CreateTime.ge(startTime);//>=

                        list = MyApplication.getDaoSession().getDataBeanDao().queryBuilder()
                                .where(conditionScoreLevel, conditionSport, conditionSay, conditionEye, conditionXuexing, conditionTime, conditionTime2)
                                .orderDesc(DataBeanDao.Properties.CreateTime)
                                .build()
                                .list();

                        if (list.size() < 1) {
                            refresh.setVisibility(View.GONE);
                            emptyLl.setVisibility(View.VISIBLE);
                        } else {
                            refresh.setVisibility(View.VISIBLE);
                            emptyLl.setVisibility(View.GONE);
                            adapter.setDataBeans(list);
                            adapter.notifyDataSetChanged();
                        }
                    }
                });
                break;
            case R.id.yuyan:
                ExamineListControl.showPopu(getContext(), yuyan, R.menu.item_yuyan, new ExamineListControl.ItemSelect() {
                    @Override
                    public void onItemSelect(MenuItem item) {
                        switch (item.getTitle().toString()) {
                            case "不能言语":
                                sayType = 1;
                                break;
                            case "只能发言":
                                sayType = 2;
                                break;
                            case "答非所问":
                                sayType = 3;
                                break;
                            case "回答不切题":
                                sayType = 4;
                                break;
                            case "回答切题":
                                sayType = 5;
                                break;
                            default:
                                sayType = 999;
                                break;
                        }
                        Log.e(TAG, "sayType: " + sayType);

                        WhereCondition conditionScoreLevel;
                        if (scoreType == 999) {
                            conditionScoreLevel = DataBeanDao.Properties.ScoreLevel.in(1, 2, 3, 4);
                        } else {
                            conditionScoreLevel = DataBeanDao.Properties.ScoreLevel.eq(scoreType);
                        }

                        WhereCondition conditionSport;
                        if (sportType == 999) {
                            conditionSport = DataBeanDao.Properties.Sport.in(1, 2, 3, 4, 5, 6);
                        } else {
                            conditionSport = DataBeanDao.Properties.Sport.eq(sportType);
                        }

                        WhereCondition conditionSay;
                        if (sayType == 999) {
                            conditionSay = DataBeanDao.Properties.Say.in(1, 2, 3, 4, 5);
                        } else {
                            conditionSay = DataBeanDao.Properties.Say.eq(sayType);
                        }

                        WhereCondition conditionEye;
                        if (eyeType == 999) {
                            conditionEye = DataBeanDao.Properties.Eye.in(1, 2, 3, 4);
                        } else {
                            conditionEye = DataBeanDao.Properties.Eye.eq(eyeType);
                        }

                        WhereCondition conditionXuexing;
                        if (bloodType.equals("999")) {
                            conditionXuexing = DataBeanDao.Properties.BloodType.in("A型", "B型", "O型", "AB型");
                        } else {
                            conditionXuexing = DataBeanDao.Properties.BloodType.eq(bloodType);
                        }

                        WhereCondition conditionTime = DataBeanDao.Properties.CreateTime.le(endTime);//<=
                        WhereCondition conditionTime2 = DataBeanDao.Properties.CreateTime.ge(startTime);//>=
                        list = MyApplication.getDaoSession().getDataBeanDao().queryBuilder()
                                .where(conditionSay, conditionScoreLevel, conditionEye, conditionXuexing, conditionSport, conditionTime, conditionTime2)
                                .orderDesc(DataBeanDao.Properties.CreateTime)
                                .build()
                                .list();
                        Log.e("onItemSelect: ", list.size() + "");
                        if (list.size() < 1) {
                            refresh.setVisibility(View.GONE);
                            emptyLl.setVisibility(View.VISIBLE);
                        } else {
                            refresh.setVisibility(View.VISIBLE);
                            emptyLl.setVisibility(View.GONE);
                            adapter.setDataBeans(list);
                            adapter.notifyDataSetChanged();
                        }

                    }
                });
                break;
            case R.id.dongzuo:
                ExamineListControl.showPopu(getContext(), dongzuo, R.menu.item_dongzuo, new ExamineListControl.ItemSelect() {
                    @Override
                    public void onItemSelect(MenuItem item) {
                        switch (item.getTitle().toString()) {
                            case "不能活动":
                                sportType = 1;
                                break;
                            case "刺痛后肢体能过度伸展":
                                sportType = 2;
                                break;
                            case "刺痛后肢体能屈曲":
                                sportType = 3;
                                break;
                            case "刺痛后能躲避":
                                sportType = 4;
                                break;
                            case "刺痛能定位":
                                sportType = 5;
                                break;
                            case "按吩咐动作":
                                sportType = 6;
                                break;
                            default:
                                sportType = 999;
                                break;
                        }
                        Log.e(TAG, "sportType: " + sportType);

                        WhereCondition conditionScoreLevel;
                        if (scoreType == 999) {
                            conditionScoreLevel = DataBeanDao.Properties.ScoreLevel.in(1, 2, 3, 4);
                        } else {
                            conditionScoreLevel = DataBeanDao.Properties.ScoreLevel.eq(scoreType);
                        }

                        WhereCondition conditionSport;
                        if (sportType == 999) {
                            conditionSport = DataBeanDao.Properties.Sport.in(1, 2, 3, 4, 5, 6);
                        } else {
                            conditionSport = DataBeanDao.Properties.Sport.eq(sportType);
                        }

                        WhereCondition conditionSay;
                        if (sayType == 999) {
                            conditionSay = DataBeanDao.Properties.Say.in(1, 2, 3, 4, 5);
                        } else {
                            conditionSay = DataBeanDao.Properties.Say.eq(sayType);
                        }

                        WhereCondition conditionEye;
                        if (eyeType == 999) {
                            conditionEye = DataBeanDao.Properties.Eye.in(1, 2, 3, 4);
                        } else {
                            conditionEye = DataBeanDao.Properties.Eye.eq(eyeType);
                        }

                        WhereCondition conditionXuexing;
                        if (bloodType.equals("999")) {
                            conditionXuexing = DataBeanDao.Properties.BloodType.in("A型", "B型", "O型", "AB型");
                        } else {
                            conditionXuexing = DataBeanDao.Properties.BloodType.eq(bloodType);
                        }

                        WhereCondition conditionTime = DataBeanDao.Properties.CreateTime.le(endTime);//<=
                        WhereCondition conditionTime2 = DataBeanDao.Properties.CreateTime.ge(startTime);//>=
                        list = MyApplication.getDaoSession().getDataBeanDao().queryBuilder()
                                .where(conditionSay, conditionScoreLevel, conditionEye, conditionXuexing, conditionSport, conditionTime, conditionTime2)
                                .orderDesc(DataBeanDao.Properties.CreateTime)
                                .build()
                                .list();

                        if (list.size() < 1) {
                            refresh.setVisibility(View.GONE);
                            emptyLl.setVisibility(View.VISIBLE);
                        } else {
                            refresh.setVisibility(View.VISIBLE);
                            emptyLl.setVisibility(View.GONE);
                            adapter.setDataBeans(list);
                            adapter.notifyDataSetChanged();
                        }
                    }
                });
                break;
            case R.id.eye:
                ExamineListControl.showPopu(getContext(), eye, R.menu.item_zhengyan, new ExamineListControl.ItemSelect() {
                    @Override
                    public void onItemSelect(MenuItem item) {
                        switch (item.getTitle().toString()) {
                            case "不睁眼":
                                eyeType = 1;
                                break;
                            case "刺痛睁眼":
                                eyeType = 2;
                                break;
                            case "呼吸睁眼":
                                eyeType = 3;
                                break;
                            case "自动睁眼":
                                eyeType = 4;
                                break;
                            default:
                                eyeType = 999;
                                break;
                        }
                        Log.e(TAG, "eyeType: " + eyeType);

                        WhereCondition conditionScoreLevel;
                        if (scoreType == 999) {
                            conditionScoreLevel = DataBeanDao.Properties.ScoreLevel.in(1, 2, 3, 4);
                        } else {
                            conditionScoreLevel = DataBeanDao.Properties.ScoreLevel.eq(scoreType);
                        }

                        WhereCondition conditionSport;
                        if (sportType == 999) {
                            conditionSport = DataBeanDao.Properties.Sport.in(1, 2, 3, 4, 5, 6);
                        } else {
                            conditionSport = DataBeanDao.Properties.Sport.eq(sportType);
                        }

                        WhereCondition conditionSay;
                        if (sayType == 999) {
                            conditionSay = DataBeanDao.Properties.Say.in(1, 2, 3, 4, 5);
                        } else {
                            conditionSay = DataBeanDao.Properties.Say.eq(sayType);
                        }

                        WhereCondition conditionEye;
                        if (eyeType == 999) {
                            conditionEye = DataBeanDao.Properties.Eye.in(1, 2, 3, 4);
                        } else {
                            conditionEye = DataBeanDao.Properties.Eye.eq(eyeType);
                        }

                        WhereCondition conditionXuexing;
                        if (bloodType.equals("999")) {
                            conditionXuexing = DataBeanDao.Properties.BloodType.in("A型", "B型", "O型", "AB型");
                        } else {
                            conditionXuexing = DataBeanDao.Properties.BloodType.eq(bloodType);
                        }

                        WhereCondition conditionTime = DataBeanDao.Properties.CreateTime.le(endTime);//<=
                        WhereCondition conditionTime2 = DataBeanDao.Properties.CreateTime.ge(startTime);//>=
                        list = MyApplication.getDaoSession().getDataBeanDao().queryBuilder()
                                .where(conditionSay, conditionScoreLevel, conditionEye, conditionXuexing, conditionSport, conditionTime, conditionTime2)
                                .orderDesc(DataBeanDao.Properties.CreateTime)
                                .build()
                                .list();
                        if (list.size() < 1) {
                            refresh.setVisibility(View.GONE);
                            emptyLl.setVisibility(View.VISIBLE);
                        } else {
                            refresh.setVisibility(View.VISIBLE);
                            emptyLl.setVisibility(View.GONE);
//                            JianceMainAdapter adapter = new JianceMainAdapter(list, getContext());
//                            rv.setAdapter(adapter);
                            adapter.setDataBeans(list);
                            adapter.notifyDataSetChanged();
                        }
                    }
                });
                break;
            case R.id.xuexing:
                ExamineListControl.showPopu(getContext(), xuexing, R.menu.item_xuexing, new ExamineListControl.ItemSelect() {
                    @Override
                    public void onItemSelect(MenuItem item) {
                        switch (item.getTitle().toString()) {
                            case "A型":
                                bloodType = "A型";
                                break;
                            case "B型":
                                bloodType = "B型";
                                break;
                            case "O型":
                                bloodType = "O型";
                                break;
                            case "AB型":
                                bloodType = "AB型";
                                break;
                            default:
                                bloodType = "999";
                                break;
                        }
                        Log.e(TAG, "bloodType: " + bloodType);

                        WhereCondition conditionScoreLevel;
                        if (scoreType == 999) {
                            conditionScoreLevel = DataBeanDao.Properties.ScoreLevel.in(1, 2, 3, 4);
                        } else {
                            conditionScoreLevel = DataBeanDao.Properties.ScoreLevel.eq(scoreType);
                        }

                        WhereCondition conditionSport;
                        if (sportType == 999) {
                            conditionSport = DataBeanDao.Properties.Sport.in(1, 2, 3, 4, 5, 6);
                        } else {
                            conditionSport = DataBeanDao.Properties.Sport.eq(sportType);
                        }

                        WhereCondition conditionSay;
                        if (sayType == 999) {
                            conditionSay = DataBeanDao.Properties.Say.in(1, 2, 3, 4, 5);
                        } else {
                            conditionSay = DataBeanDao.Properties.Say.eq(sayType);
                        }

                        WhereCondition conditionEye;
                        if (eyeType == 999) {
                            conditionEye = DataBeanDao.Properties.Eye.in(1, 2, 3, 4);
                        } else {
                            conditionEye = DataBeanDao.Properties.Eye.eq(eyeType);
                        }

                        WhereCondition conditionXuexing;
                        if (bloodType.equals("999")) {
                            conditionXuexing = DataBeanDao.Properties.BloodType.in("A型", "B型", "O型", "AB型");
                        } else {
                            conditionXuexing = DataBeanDao.Properties.BloodType.eq(bloodType);
                        }

                        WhereCondition conditionTime = DataBeanDao.Properties.CreateTime.le(endTime);//<=
                        WhereCondition conditionTime2 = DataBeanDao.Properties.CreateTime.ge(startTime);//>=
                        list = MyApplication.getDaoSession().getDataBeanDao().queryBuilder()
                                .where(conditionSay, conditionScoreLevel, conditionEye, conditionXuexing, conditionSport, conditionTime, conditionTime2)
                                .orderDesc(DataBeanDao.Properties.CreateTime)
                                .build()
                                .list();
                        if (list.size() < 1) {
                            refresh.setVisibility(View.GONE);
                            emptyLl.setVisibility(View.VISIBLE);
                        } else {
                            refresh.setVisibility(View.VISIBLE);
                            emptyLl.setVisibility(View.GONE);
                            adapter.setDataBeans(list);
                            adapter.notifyDataSetChanged();
                        }
                    }
                });
                break;
            case R.id.cancel_tv:
                changanRl.setVisibility(View.GONE);
                selectRl.setVisibility(View.VISIBLE);
                adapter.setShowCheckBox(false);
                isShowCheckBox = !isShowCheckBox;
                refreshUI();
                break;
            case R.id.selectall_tv:
                adapter.selectAll();
                break;
//            case R.id.daochu_tv:
//                adapter.neverall();
//                break;
            case R.id.search_img:
                searchEt.setText("");
                list = MyApplication.getDaoSession().getDataBeanDao().queryBuilder()
                        .orderDesc(DataBeanDao.Properties.CreateTime)
                        .build().list();

                adapter.setDataBeans(list);

                adapter.notifyDataSetChanged();

                selectRl.setVisibility(View.GONE);
                searchRl.setVisibility(View.VISIBLE);

                break;
            case R.id.cancel_search_tv:
                searchEt.setText("");
                list = MyApplication.getDaoSession().getDataBeanDao().queryBuilder()
                        .orderDesc(DataBeanDao.Properties.CreateTime)
                        .build().list();

                adapter.setDataBeans(list);

                adapter.notifyDataSetChanged();
                selectRl.setVisibility(View.VISIBLE);
                searchRl.setVisibility(View.GONE);
                break;
            case R.id.date:
                MyPopu myPopu = new MyPopu(getContext(), date);
                myPopu.showAsDropDown(v);
                break;
            case R.id.shanchu_tv:
//                MyApplication.getDaoSession().getDataBeanDao().delete
                adapter.deleteData();
                break;
        }
    }

    private void refreshUI() {
        if (adapter == null) {
            adapter = new JianceMainAdapter(list, getContext());
            rv.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onResume() {
        super.onResume();

        list = MyApplication.getDaoSession().getDataBeanDao().queryBuilder().orderDesc(DataBeanDao.Properties.CreateTime).list();
//        Log.e(TAG, "onResume: aaaa---" + list.get(0).getCreateTime());
        if (list.size() == 0) {
            emptyLl.setVisibility(View.VISIBLE);
            refresh.setVisibility(View.GONE);
        } else {
            emptyLl.setVisibility(View.GONE);
            refresh.setVisibility(View.VISIBLE);
        }
        initRvAdapter();

    }
}
